package Frame.screen;

import FCRendering.FCRMaster;
import FCRendering.ImageUtil;
import Function.FCMutilPlayCache;
import VWorldGameStudio.FCMap;
import VWorldGameStudio.FCPlayerInfo;

import java.awt.*;
import java.util.function.BiConsumer;

import static FCRendering.FCRMaster.fcv;
import static FCRendering.FCRMaster.player1;

public class WorldMapScreen extends IScreen {

    @Override
    public void dispatchPaintComponent(Graphics g, FCRMaster fcrMaster) {
        if (FCRMaster.fcv.isShowMapImg()) {
            FCMap map = FCRMaster.mapLib.getMapOfName(player1.getMapname());
            int y = (int) (60 * fcv.getProportion());
            int w = (int) ((map.getMapHeight() + 1) * 20 * fcv.getProportion());
            int h = (int) ((map.getMapWidth() + 1) * 20 * fcv.getProportion());
            int x = (int) (1920 * fcv.getProportion() - w) / 2;
            if (map.getMapWidth() > 100) {
                //TODO 大地图
                x = (int) ((1920 - 900) / 2 * fcv.getProportion());
                y = (int) (10 * fcv.getProportion());
                h = (int) (900 * fcv.getProportion());
                w = (int) (900 * fcv.getProportion());
            }
            float px = (float) w / map.getMapHeight();
            float py = (float) h / map.getMapWidth();
            g.drawImage(ImageUtil.getMapImage(player1.getMapname()), x, y, w, h, this);
            int finalX = x;
            int finalY = y;
            FCMutilPlayCache.gamePlayers.forEach(new BiConsumer<String, FCPlayerInfo>() {
                @Override
                public void accept(String s, FCPlayerInfo playerInfo) {
                    if (!playerInfo.isANpcPlayer()) {
                        if (playerInfo.getMapname().equals(player1.getMapname())) {
                            g.drawImage(ImageUtil.getImage("positionflag"), finalX + (int) (px * playerInfo.getPlayer1y()), finalY + (int) (py * playerInfo.getPlayer1x()), (int) (14 * fcv.getProportion()),
                                    (int) (14 * fcv.getProportion()), WorldMapScreen.this);
                        } else if (player1.getMapname().equals("world")) {
                            //TODO 要是联机玩家不在大地图上，在小地图需要映射到大地图
                            if(playerInfo.getLastInWorldMapX() != -1){
                                g.drawImage(ImageUtil.getImage("positionflag"), finalX + (int) (px * playerInfo.getLastInWorldMapY()), finalY + (int) (py * playerInfo.getLastInWorldMapX()), (int) (14 * fcv.getProportion()),
                                        (int) (14 * fcv.getProportion()), WorldMapScreen.this);
                            }
                        }
                    }
                }
            });

            g.drawImage(ImageUtil.getImage("positionflag"), x + (int) (px * player1.getPlayer1y()), y + (int) (py * player1.getPlayer1x()), (int) (16 * fcv.getProportion()),
                    (int) (16 * fcv.getProportion()), this);
        }
    }

    @Override
    protected void setBounds() {

    }


}
